package com.qf.dao.DaoImpl;

import com.qf.dao.ArchivesDao;
import com.qf.pojo.Archives;
import com.qf.pojo.Page;
import com.qf.util.DBUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * @author 86198
 * @version V1.0
 * @Project Java_qf
 * @Package com.qf.dao.DaoImpl
 * @Description:
 * @Date 2022/5/27 20:29
 */
public class ArchivesDaoImpl implements ArchivesDao {
    private QueryRunner qr = new QueryRunner(DBUtil.getDateSource());
    @Override
    public List<Archives> showAll(Page page) {
        String sql = "select * from record limit ?,?";
        try {
            return qr.query(sql, new BeanListHandler<Archives>(Archives.class),page.getPage_start(),page.getPage_number());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int totalCounts() {
        String sql = "select count(*) from record";
        try {
            long query = qr.query(sql, new ScalarHandler<Long>());
            return (int)query;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public void update(Archives archives) {
        String sql = "update record set  username=?,birthday=?,gender=?,occupation=?,abode=?,PhoneNumber=?,picture=?  where id=?";
        System.out.println(archives);
        try {
            qr.update(sql, archives.getUsername(), archives.getBirthday(), archives.getGender(), archives.getOccupation(), archives.getAbode(), archives.getPhoneNumber(), archives.getPicture(), archives.getId());
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public Archives showOne(int id) {
        String sql = "select * from record where id=?";
        try {
            return qr.query(sql, new BeanHandler<Archives>(Archives.class), id);
        } catch (SQLException e) {
            e.printStackTrace();
        }


        return null;
    }
}
